package com.example.demo1.iot;

import com.example.demo1.core.entity.iot.Weather;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/Weather")
@RestController
public class WeatherApi {
	@Autowired
	private WeatherService weatherService;

	/**
	 * create database and table
	 * 
	 * @return
	 */
	@GetMapping("/init")
//	@DataSource(name = DataSourcesName.TAOS)
	public String init() {
		 weatherService.init();
		Map<String, Object> result = new HashMap<>();

		return "ok";
	}

	/**
	 * Pagination Query
	 * 
	 * @param limit
	 * @param offset
	 * @return
	 */
	@GetMapping("/{limit}/{offset}")
//	@DataSource(name = DataSourcesName.TAOS)
	public List<Weather> queryWeather(@PathVariable Long limit, @PathVariable Long offset) {
		return weatherService.query(limit, offset);
	}

	/**
	 * upload single weather info
	 * 
	 * @param temperature
	 * @param humidity
	 * @return
	 */
	@PostMapping("/{temperature}/{humidity}")
//	@DataSource(name = DataSourcesName.TAOS)
	public int saveWeather(@PathVariable int temperature, @PathVariable float humidity) {

		return weatherService.save(temperature, humidity);
	}

	/**
	 * upload multi weather info
	 * 
	 * @param weatherList
	 * @return
	 */
	@PostMapping("/batch")
//	@DataSource(name = DataSourcesName.TAOS)
	public int batchSaveWeather(@RequestBody List<Weather> weatherList) {

		return weatherService.save(weatherList);
	}
}
